package com.fira.api.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.fira.api.domain.dto.LoginDTO;
import com.fira.api.domain.dto.SysBizUserDTO;
import com.fira.api.service.IUserService;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.core.domain.platform.vo.PlatformTenantVO;
import com.fira.common.core.domain.saas.dto.SysUserDTO;
import com.fira.common.core.domain.saas.vo.SysUserVO;
import com.fira.common.utils.StringUtils;
import com.fira.edu.domain.EduParent;
import com.fira.edu.service.IEduParentService;
import com.fira.platform.service.IPlatformTenantService;
import com.fira.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author henryGui
 * @version V1.0
 * @Title: UserServiceImpl
 * @Package com.fira.api.service.impl
 * @Description: TODO
 * @date 2021-10-13 下午2:30
 */
@Service
public class UserServiceImpl implements IUserService {


    @Autowired
    private IEduParentService iEduParentService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IPlatformTenantService tenantService;

    @Override
    public AjaxResult findUserByPhone(String phoneNumber, LoginDTO loginDTO) {

        HashMap<String, String> tenants = new HashMap<>();
        List<String> strs = new ArrayList<>();
        strs.add(phoneNumber);
        List<EduParent> eduParents = iEduParentService.selectEduParentListByPhones(strs);
        if (CollectionUtil.isNotEmpty(eduParents)) {
            loginDTO.setParentId(eduParents.get(0).getId());
            loginDTO.setParentName(eduParents.get(0).getParentName());
        }
        List<SysUserVO> sysUserVOS = iSysUserService.selectUserListByPhoneNumber(phoneNumber);
        if (CollectionUtil.isNotEmpty(sysUserVOS)) {
            for (SysUserVO sysUserVO : sysUserVOS) {
                tenants.put(sysUserVO.getTenantId(), "");
            }
            loginDTO.setCurrentTenantId(sysUserVOS.get(0).getTenantId());
            loginDTO.setTeacherId(sysUserVOS.get(0).getId());
            loginDTO.setTeacherName(sysUserVOS.get(0).getUserName());
        }
        for (String key : tenants.keySet()) {
            PlatformTenantVO platformTenantVO = tenantService.selectPlatformTenantById(key);
            if (platformTenantVO != null) {
                tenants.put(key, platformTenantVO.getTenantName());
            }
        }
        //todo ====begin=====此次为了临时联调接口将查询的租户信息重新放入redis 以便loginDto能获取到租户id
        System.out.println("----->>" + loginDTO.getToken());
        stringRedisTemplate.opsForValue().set(loginDTO.getToken(), JSONObject.toJSONString(loginDTO));
        //todo =====end====此次为了临时联调接口将查询的租户信息重新放入redis

        //TODO 填充权限标志,后期完善
        if (loginDTO.getTeacherId() == null && loginDTO.getParentId() == null) {
            return AjaxResult.error("您的手机号码未登记！");
        }
        return AjaxResult.success(loginDTO);
    }

    @Override
    public AjaxResult selectUserListByObj(LoginDTO loginDTO, SysBizUserDTO bizUserDTO) {
        if (loginDTO == null || bizUserDTO == null || StringUtils.isBlank(loginDTO.getCurrentTenantId())) {
            return AjaxResult.error("不是租户用户无法查询当前机构下的用户信息");
        }
        SysUserDTO userDTO = new SysUserDTO();
        BeanUtils.copyProperties(bizUserDTO, userDTO);
        userDTO.setTenantId(loginDTO.getCurrentTenantId());
        List<SysUserVO> list = iSysUserService.selectUserListByObj(userDTO);
        return AjaxResult.success("查询成功", list);
    }


}
